package com.sky.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sky.constant.StatusConstant;
import com.sky.dto.CategoryDTO;
import com.sky.dto.CategoryPageQueryDTO;
import com.sky.entity.Category;
import com.sky.exception.BusinessException;
import com.sky.mapper.CategoryMapper;
import com.sky.result.PageResult;
import com.sky.service.CategoryService;
import com.sky.service.DishService;
import com.sky.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CategoryServiceImpl implements CategoryService {
    @Autowired
    private CategoryMapper categoryMapper;
    @Autowired
    private DishService dishService;
    @Autowired
    private SetmealService setmealService;

    @Override
    public PageResult findByPage(CategoryPageQueryDTO categoryDTO) {
        int page = categoryDTO.getPage();
        int pageSize = categoryDTO.getPageSize();
        if(page<=0){
            page=1;
        }
        if(pageSize<=0){
            pageSize=10;
        }
        PageHelper.startPage(page,pageSize);
        List<Category> list = categoryMapper.findList(categoryDTO);
        PageInfo<Category> pageInfo = PageInfo.of(list);
        return new PageResult(pageInfo.getTotal(),list);
    }

    @Override
    public void save(CategoryDTO categoryDTO) {
        String name = categoryDTO.getName();
        Integer sort = categoryDTO.getSort();


        if(!name.matches("[a-zA-Z0-9\u4e00-\u9fa5]{2,20}") ||StrUtil.isBlank(name)){
            throw new BusinessException("分类名称输入不符，请输入2-20个字符");
        }
        if(sort==null){
            throw new BusinessException("排序不能为空");
        }
        if(sort<0 ||sort>99){
            throw new BusinessException("排序只能输入整数类型");
        }

       Category category = categoryMapper.findByName(name);
       if(category!=null){
           throw new BusinessException("已存在该分类");
       }
       category=new Category();
        BeanUtil.copyProperties(categoryDTO,category);
        category.setStatus(StatusConstant.DISABLE);
       categoryMapper.save(category);

    }

    @Override
    public void deleteById(Long id) {
     Integer count1 = dishService.findByCategoryId(id);
     if(count1 >0){
         throw new BusinessException("分类下有产品不可删除");
     }
     Integer count2 = setmealService.findByCategoryId(id);
        if(count2 >0){
         throw new BusinessException("分类下有产品不可删除");
        }
        categoryMapper.deleteById(id);

    }

    @Override
    public List<Category> findByList(Integer type) {
        CategoryPageQueryDTO categoryDTO =new CategoryPageQueryDTO();
        categoryDTO.setType(type);
        List<Category> list = categoryMapper.findList(categoryDTO);
        return list;
    }

    @Override
    public void updateStatus(Integer status, Long id) {
        categoryMapper.udpateStatus(status,id);
    }

    @Override
    public void update(CategoryDTO categoryDTO) {

        String name = categoryDTO.getName();
        Integer sort = categoryDTO.getSort();
        if(!name.matches("[a-zA-Z0-9\u4e00-\u9fa5]{2,20}") ||StrUtil.isBlank(name)){
            throw new BusinessException("分类名称输入不符，请输入2-20个字符");
        }
        if(sort==null){
            throw new BusinessException("排序不能为空");
        }
        if(sort<0 ||sort>99){
            throw new BusinessException("排序只能输入整数类型");
        }
        Category category = categoryMapper.findByName(name);
        if(category!=null && category.getId()!= categoryDTO.getId()){
            throw new BusinessException("菜名已存在");
        }
        category=new Category();
        BeanUtil.copyProperties(categoryDTO,category);

        categoryMapper.update(category);
    }

    @Override
    public List<Category> findListByCategory4App(Category category) {
          List<Category> list =  categoryMapper.findListByCategory4App(category);
          return list;
    }
}
